<?php
//namespace student\controller\welcome;
defined('BASEPATH') OR exit('No direct script access allowed');

class Getuser extends CI_Model {


	public function __construct()
	{
		parent::__construct();
		$this->load->helper('url_helper');
	}

    /**
	* 验证用户登录
    */
	public function verifyLogin($username='',$password='')
	{
		$sql = "select * from user_info where tel=? and password=?";
		$param = array($username,$password);
		$res = $this->db->query($sql,$param)->row_array();
		return $res;
	}

    /**
    ** 通过手机号验证用户是否存在
    **/
	public function verifyUser($username)
	{
		$sql = "select * from user_info where tel=?";
		$res = $this->db->query($sql,$username)->row_array();
		return $res;
	}

    /**
    ** 添加新用户  手机号+密码
    **/
	public function addUser($tel='',$password='')
	{
		if($tel=='' || $password == '')
		{
			return false;
		}
		$sql = "insert into user_info (tel,password,register_time) value (?,?,?)";
		$param = array($tel,$password,date("Y-m-d H:i:s"));
		$res = $this->db->query($sql,$param);

		$sql = "insert into user_account (user_id,balance) value (?,?)"; //新建账户余额表
		$param = array($this->db->insert_id(),0.00);
		$res = $this->db->query($sql,$param);

		return $this->db->insert_id();
	}

	/**
	** 修改用户名  非手机号，手机号注册后不可修改
	**/
	public function changeUserName($newName)
	{
		$tel = $_SESSION['user_name'];
		$user_id = $_SESSION['user_id'];

		$sql = "update user_info set user_name = '$newName' where id = $user_id and tel = '$tel'";
		$res = $this->db->query($sql);
		return $res;
	}

	/**
	** 根据用户tel 获取用户信息
	**/
	 public function getUserInfo($tel)
	{
	 	$sql = "select * from user_info where tel = ?";
	 	$res = $this->db->query($sql,$tel)->row_array(); 
	 	return $res;
	}


	/**
	*  个人信息认证，补充用户邮箱、行业、真实用户名
	**/
	public function modifyUserInfo($user_name,$email,$vocation)
	{
		switch($vocation){
			case 1:$vocation = "互联网行业(IT、电子、互联网)";break;
			case 2:$vocation = "IT行业";break;
			case 3:$vocation = "电子行业";break;
			default:$vocation = "未知行业";
		}
		$user_id = $_SESSION['user_id'];
		$sql = "update user_info set user_name = ?,email = ?,vocation = ?,lastoperate_time = ?,update_time = ? where id = ?";
		$array = array($user_name,$email,$vocation,date("Y-m-d H:i:s"),date("Y-m-d H:i:s"),$user_id);
		$res = $this->db->query($sql,$array);
		return $res;
	}

	/**
	** 企业信息认证，添加企业信息
	**/
	public function modifyCompany($array)
	{
		$array['update_time'] = date("Y-m-d H:i:s");
		$sql = "insert into company_info (tel,email,company_name,boss_name,business_scope,license_id,vocation,business_name,update_time) values(?,?,?,?,?,?,?,?,?)";
		$res = $this->db->query($sql,$array);
		return $res;
	}

	/**
	** 修改用户密码
	**/
	public function changePass($newpass)
	{
		$sql = "update user_info set password = ? where id = ?";
		$array = array($newpass,$_SESSION['user_id']);
		$res = $this->db->query($sql,$array);
		return $res;
	}

	/**
	** 通过用户id获取账户余额
	**/
	public function getBalance($user_id)
	{
		$user_id = $_SESSION['user_id'];
		$sql = "select balance from user_account where user_id = ?";
		$res = $this->db->query($sql,$user_id)->row_array();
		return $res['balance'];
	}

	/**
	* 通过用户id 更新账户余额
	**/
	public function updateBalance($user_id,$new_balance,$last_use_id)
	{
		$sql = "update user_account set balance = ? and last_use_id = ? where user_id = ?";
		$array = array($new_balance,$last_use_id,$user_id);
		$res = $this->db->query($sql,$array);
		return $res;
	}
}
